const fs = require('fs');
const path = require('path');

const FILES = [
  path.join(process.cwd(), 'data', 'json-database.json'),
  path.join(process.cwd(), 'data', 'tables.json'),
];

function convertDeviceIdToNumber(devices) {
  return devices.map((device, idx) => {
    // 设备ID从1开始递增
    const newId = idx + 1;
    // 兼容不同字段名
    if (device.device_id !== undefined) device.device_id = newId;
    if (device.id !== undefined && typeof device.id === 'string') device.id = newId;
    return device;
  });
}

FILES.forEach(filePath => {
  if (!fs.existsSync(filePath)) return;
  const content = fs.readFileSync(filePath, 'utf-8');
  const json = JSON.parse(content);
  if (Array.isArray(json.devices)) {
    json.devices = convertDeviceIdToNumber(json.devices);
    fs.writeFileSync(filePath, JSON.stringify(json, null, 2));
    console.log(`${filePath} 设备ID已全部转换为数字`);
  }
}); 